package com.monitor.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.monitor.entity.ErpContract;
import com.monitor.service.ErpContractService;
import com.monitor.service.ErpCustomerService;
import com.monitor.util.DataUtil;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 合同协议 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-17
 */
@RestController
@RequestMapping("/erpContract")
@Api(description="合同协议-协议信息")
public class ErpContractController {

    @Resource
    private ErpContractService contractService;
    @Resource
    private ErpCustomerService customerService;

    @RequestMapping("/inertOrUpdateContract")
    @ApiOperation("添加Or修改协议信息信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Integer> inertOrUpdateContract(ErpContract contract) {
        Result<Integer> result=new Result<Integer>();
        try{
        	String code=customerService.selectById(contract.getCustomerId()).getCusCode().toString();
        	List<ErpContract> list=contractService.selectList(new EntityWrapper<ErpContract>().like("con_name", code).orderBy("id", false));
        	if(list.size()>0) {
        		String[] name=list.get(0).getConCode().split("-");
        		int shu=Integer.parseInt(name[1])+1;
        		code=code+"-"+shu;
        	}else {
        		code=code+"-1";
        	}
            contract.setConCode(code);
            contract.setConDate(DataUtil.getStringDate());
            boolean count=contractService.insertOrUpdate(contract);
            result.setCount(1);
            result.setData(contract.getId());
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getContractById")
    @ApiOperation("根据协议id协议信息信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "contractId", value = "协议ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<ErpContract> getContractById(Integer contractId) {
        Result<ErpContract> result=new Result<ErpContract>();
        try{
            result.setCount(1);
            result.setData(contractService.selectById(contractId));
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/removeContractById")
    @ApiOperation("根据协议id删除协议信息信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "contractId", value = "协议ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> removeContractById(Integer contractId) {
        Result<Integer> result=new Result<Integer>();
        try{
            result.setCount(1);
            result.setData(contractService.deleteById(contractId)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getContractBycustIdPage")
    @ApiOperation("根据客户ID查询协议信息分页")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Page<ErpContract>> getContractBycustIdPage(@RequestParam(value = "page", defaultValue = "1") int page,
                                                        @RequestParam(value="limit",defaultValue="10") int limit, Integer custId) {
        Result<Page<ErpContract>> result=new Result<Page<ErpContract>>();
        try{
            Page<ErpContract> page1=new Page<ErpContract>(page,limit);
            Page<ErpContract> carPage=contractService.selectPage(page1,new EntityWrapper<ErpContract>().eq("customer_id",custId));
            result.setCount(carPage.getRecords().size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getContractBycustIdList")
    @ApiOperation("根据客户ID查询所有协议信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpContract>> getContractBycustIdList(Integer custId) {
        Result<List<ErpContract>> result=new Result<List<ErpContract>>();
        try{
            List<ErpContract> carPage=contractService.selectList(new EntityWrapper<ErpContract>().eq("customer_id",custId));
            result.setCount(carPage.size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }




}

